###FIGURES###

load("data_combined.RData")
load("uk.RData")

##Figure 1

## Australia
data_combined$treatment[data_combined$tr_platform==0 & data_combined$tr_interview==0]<-"YT Weather"
data_combined$treatment[data_combined$tr_platform==1 & data_combined$tr_interview==0]<-"SN Weather"
data_combined$treatment[data_combined$tr_platform==1 & data_combined$tr_interview==1]<-"SN Interview"
data_combined$treatment[data_combined$tr_platform==0 & data_combined$tr_interview==1]<-"YT Interview"

data_combined$condition1<-factor(data_combined$treatment)

df1 <- data_combined %>% 
  group_by(condition1) %>%  
  mutate(condition1 = fct_relevel(condition1, 
                                  "YT Weather", "SN Weather", "YT Interview", 
                                  "SN Interview"))   %>%
  summarise( 
    n=n(),
    mean=mean(attitudes_scale, na.rm=TRUE),
    sd=sd(attitudes_scale, na.rm=TRUE)
  ) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1))


aus1 <- ggplot(df1) +
  geom_bar(aes(x = condition1, y = mean), stat = "identity", fill = "grey", alpha = 0.5) +
  geom_errorbar(aes(x = condition1, ymin = mean - ic, ymax = mean + ic), width = 0.15, colour = "black", alpha = 0.7, size = 1) +
  geom_text(aes(x = condition1, y = mean, label = round(mean, 2)), vjust = -2.0, size = 3, color = "black") +  # Add means as text labels on top of the bars
  scale_y_continuous(limits = c(0, 0.8)) +
  ggtitle("Australia") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme_bw() +
  labs(x = "", y = "Agree with ER statements")



##UK
uk$condition<-uk$treatment
uk$condition[uk$condition=="YT Weather"]<-"YT Weather"
uk$condition[uk$condition=="SN Weather"]<-"SN Weather"
uk$condition[uk$condition=="Unchallenged SN Itw"]<-"SN Int Unc"
uk$condition[uk$condition=="Challenged SN Itw"]<-"SN Int Chal"
uk$condition[uk$condition=="Unchallenged YT Itw"]<-"YT Int Unc"
uk$condition[uk$condition=="Challenged YT Itw"]<-"YT Int Chal"

uk$condition<-factor(uk$condition)

df2 <- uk %>% 
  group_by(condition) %>% 
  mutate(condition = fct_relevel(condition, 
                                 "YT Weather", "SN Weather", "YT Int Unc", "SN Int Unc", "YT Int Chal", "SN Int Chal" ))   %>%
  summarise( 
    n=n(),
    mean=mean(attitudes_scale),
    sd=sd(attitudes_scale)
  ) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1))

uk1 <- ggplot(df2) +
  geom_bar( aes(x=condition, y=mean), stat="identity", fill="grey", alpha=0.5) +  scale_y_continuous(limits = c(0, .8))+
  geom_errorbar( aes(x=condition, ymin=mean-ic, ymax=mean+ic), width=0.2, colour="black", alpha=0.8, size=1) +
  geom_text(aes(x = condition, y = mean, label = round(mean, 2)), vjust = -3.0, size = 3, color = "black") +  # Add means as text labels on top of the bars
  ggtitle("United Kingdom")  +
  theme(plot.title = element_text(hjust = 0.5)) + theme_bw()+ labs(x="", y="Agree with ER statements") 

##Combine figures
jpeg(file="attitudes.jpeg",  width = 800, height = 600)
p = list(aus1,uk1) %>% map(~.x + labs(y=NULL))
yleft = richtext_grob("Agree with Extreme Right statements", rot=90)
attitudes <- grid.arrange(grobs=p, ncol = 2, nrow = 1, left = yleft)
dev.off()


##Figure 2

df3 <- data_combined %>% 
  group_by(condition1) %>% 
  mutate(condition1 = fct_relevel(condition1, 
                                  "YT Weather", "SN Weather", "YT Interview", 
                                  "SN Interview"))   %>% summarise( 
                                    n=n(),
                                    mean=mean(norms_scale, na.rm=TRUE),
                                    sd=sd(norms_scale, na.rm=TRUE)
                                  ) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1))

aus2 <- ggplot(df3) +
  geom_bar( aes(x=condition1, y=mean), stat="identity", fill="grey", alpha=0.5) +  scale_y_continuous(limits = c(0, .8))+
  geom_text(aes(x = condition1, y = mean, label = round(mean, 2)), vjust = -3.0, size = 3, color = "black") +  # Add means as text labels on top of the bars
  geom_errorbar( aes(x=condition1, ymin=mean-ic, ymax=mean+ic), width=0.2, colour="black", alpha=0.7, size=1) +
  ggtitle("Australia") + theme(plot.title = element_text(hjust = 0.5)) + theme_bw()+ labs(x="", y="Belief others agree with ER statements") 


df4 <- uk %>% 
  group_by(condition) %>% 
  mutate(condition = fct_relevel(condition, 
                                 "YT Weather", "SN Weather", "YT Int Unc", "SN Int Unc", "YT Int Chal", "SN Int Chal" ))   %>%
  summarise( 
    n=n(),
    mean=mean(norms_scale, na.rm=TRUE),
    sd=sd(norms_scale, na.rm=TRUE)
  ) %>%
  mutate( se=sd/sqrt(n))  %>%
  mutate( ic=se * qt((1-0.05)/2 + .5, n-1))

uk2 <- ggplot(df4) +
  geom_bar( aes(x=condition, y=mean), stat="identity", fill="grey", alpha=0.5) +   scale_y_continuous(limits = c(0, .8))+
  geom_errorbar( aes(x=condition, ymin=mean-ic, ymax=mean+ic), width=0.2, colour="black", alpha=0.8, size=1) +
  geom_text(aes(x = condition, y = mean, label = round(mean, 2)), vjust = -3.0, size = 3, color = "black") +  # Add means as text labels on top of the bars
  ggtitle("United Kingdom") + theme(plot.title = element_text(hjust = 0.5)) + theme_bw()+ labs(x="", y="Belief others agree with ER statements") 



#Combine figures

jpeg(file="norms.jpeg",  width = 800, height = 600)
p1 = list(aus2,uk2) %>% map(~.x + labs(y=NULL))
yleft1 = richtext_grob("Believe others agree with Extreme Right statements", rot=90)
norms <- grid.arrange(grobs=p1, ncol = 2, nrow = 1, left = yleft1)
dev.off()


##Figure 3

# Australia

data_combined$treatment[data_combined$tr_platform==0 & data_combined$tr_interview==0]<-"1. YT Weather"
data_combined$treatment[data_combined$tr_platform==1 & data_combined$tr_interview==0]<-"2. SN Weather"
data_combined$treatment[data_combined$tr_platform==0 & data_combined$tr_interview==1]<-"3. YT Interview"
data_combined$treatment[data_combined$tr_platform==1 & data_combined$tr_interview==1]<-"4. SN Interview"

AUS_subset <- data_combined %>% select(treatment, Cottrell_respectable, Cottrell_unrespectable, Cottrell_dontknow)

AUS_subset$treatment<-factor(AUS_subset$treatment)


# Calculate means and CIs
AUS_long <- AUS_subset %>% 
  tidyr::gather(key = "outcome", value = "value", 
                Cottrell_respectable, Cottrell_unrespectable, Cottrell_dontknow) %>%
  group_by(treatment, outcome) %>%
  summarize(mean = mean(value), 
            lower_ci = mean - qt(0.975, df = n() - 1) * (sd(value) / sqrt(n())),
            upper_ci = mean + qt(0.975, df = n() - 1) * (sd(value) / sqrt(n())))

# Create plot

fig_actor_AUS <- ggplot(AUS_long, aes(x = treatment, y = mean, fill = outcome)) +
  geom_bar(stat = "identity", position = "dodge", color = "black") +
  geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci), 
                width = 0.2, position = position_dodge(0.9), color = "black") +
  geom_text(aes(label = round(mean, 2)), vjust = -2.0, position = position_dodge(width = 0.9), size = 3) +
  labs(x = "Experimental Condition", y = "Respectability of actor") +
  scale_fill_manual(values = c("#2B2B2B", "#808080", "#D3D3D3"),
                    labels = c("don't know Cottrell", "Cottrell respectable", "Cottrell unrespectable")) +
  theme_classic() +
  scale_x_discrete(labels = c("YT Weather", "Sky Weather", "YT Interview", "Sky Interview"))

ggsave("fig_actor_AUS.pdf",
       fig_actor_AUS,
       width = 9,
       height = 6)


# UK

uk$treatment<-uk$treatment
uk$treatment[uk$treatment=="YT Weather"]<-"1. YT Weather"
uk$treatment[uk$treatment=="SN Weather"]<-"2. SN Weather"
uk$treatment[uk$treatment=="Unchallenged YT Itw"]<-"3. YT Int Unc"
uk$treatment[uk$treatment=="Unchallenged SN Itw"]<-"4. SN Int Unc"
uk$treatment[uk$treatment=="Challenged YT Itw"]<-"5. YT Int Chal"
uk$treatment[uk$treatment=="Challenged SN Itw"]<-"6. SN Int Chal"

uk_subset <- uk %>% select(treatment, Robinson_respectable:Robinson_dontknow)

# Calculate means and CIs
uk_long <- uk_subset %>% 
  tidyr::gather(key = "outcome", value = "value", 
                Robinson_respectable:Robinson_dontknow) %>%
  group_by(treatment, outcome) %>%
  summarize(mean = mean(value), 
            lower_ci = mean - qt(0.975, df = n() - 1) * (sd(value) / sqrt(n())),
            upper_ci = mean + qt(0.975, df = n() - 1) * (sd(value) / sqrt(n())))

# Create plot
fig_actor_UK <- ggplot(uk_long, aes(x = treatment, y = mean, fill = outcome)) +
  geom_bar(stat = "identity", position = "dodge", color = "black") +
  geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci), 
                width = 0.2, position = position_dodge(0.9), color = "black") +
  geom_text(aes(label = round(mean, 2)), vjust = -2.6, position = position_dodge(width = 0.9), size = 3) +
  labs(x = "Experimental Condition", y = "Respectability of actor") +
  scale_fill_manual(values = c("#2B2B2B", "#808080", "#D3D3D3"),
                    labels = c("don't know Robinson", "Robinson respectable", "Robinson unrespectable")) +
  theme_classic() +
  scale_x_discrete(labels = c("YT Weather", "Sky Weather", "YT Int Unch", "Sky Int Unch", "YT Int Chal", "Sky Int Chal"))

ggsave("fig_actor_UK.pdf",
       fig_actor_UK,
       width = 9,
       height = 6)

#Figure 4


#Australia

interf<-subset(data_combined, select = (c(attitudes_scale, tr_interview, attitudes_pre_scale, norms_scale, tr_platform, wave, age)))

interf$Y1<-interf$attitudes_scale

interf$D<-interf$tr_interview

interf$X1<-interf$attitudes_pre_scale
interf$X2<-as.numeric(interf$age)

interf$Z1<-interf$tr_platform
interf$Z2<-interf$wave

interf1<-as.data.frame(interf)

#Moderator: libertarian-authoritarian attitudes

jpeg(file="attitudesinter2_AUS.jpeg",  width = 800, height = 600)
interflex(Y = "Y1", D = "D", X = "X1",  Z=c("Z1", "Z2"), estimator = 'binning', theme.bw = TRUE, data=interf1, xlab=("moderator = authoritarian attitudes"), ylab=("CATE of interview on attitudes"), na.rm = TRUE) 
dev.off()

#Moderator: Age

jpeg(file="ageinter2_AUS.jpeg",  width = 800, height = 600)
interflex(Y = "Y1", D = "Z1", X = "X2",  Z=c("Z2"), estimator = 'binning', theme.bw = TRUE, data=interf1, xlab=("moderator = age in years"), ylab=("CATE of platform on attitudes"), na.rm = TRUE)
dev.off()



#UK

#Moderator: libertarian-authoritarian attitudes

interf2<-subset(uk, (tr_challenged!=1), select = (c(attitudes_scale, tr_interview, tr_challenged, attitudes_pre_scale, norms_scale, tr_platform, age_num, sex, education, party)))

interf2$Y1<-interf2$attitudes_scale

interf2$D<-interf2$tr_interview

interf2$X1<-interf2$attitudes_pre_scale
interf2$X2<-interf2$age_num

interf2$Z1<-interf2$tr_platform

interf_uk<-as.data.frame(interf2)

jpeg(file="attitudesinter2_GB.jpeg",  width = 800, height = 600)
interflex(Y = "Y1", D = "D", X = "X1",  Z=c("Z1"), estimator = 'binning', theme.bw = TRUE,data=interf_uk, xlab=("moderator = authoritarian attitudes"), ylab=("CATE of interview on attitudes"), na.rm = TRUE)
dev.off()


#Moderator: Age

interf3<-subset(uk, (tr==3 | tr==4), select = (c(attitudes_scale, tr_interview, attitudes_pre_scale, norms_scale, tr_platform, age_num, sex, education, party)))

interf3$Y1<-interf3$attitudes_scale

interf3$D<-interf3$tr_interview

interf3$X1<-interf3$attitudes_pre_scale
interf3$X2<-interf3$age_num

interf3$Z1<-interf3$tr_platform

interf_uk2<-as.data.frame(interf3)

jpeg(file="ageinter2_UK.jpeg",  width = 800, height = 600)
interflex(Y = "Y1", D = "Z1", X = "X2", estimator = 'binning', theme.bw = TRUE, data=interf_uk2, xlab=("moderator = age"), ylab=("CATE of platform type on attitudes"), na.rm = TRUE)
dev.off()



